[[...path]].page.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import React from 'react';
  2. import { IUserHasId } from '@growi/core';
  3. import {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import dynamic from 'next/dynamic';
  8. import Head from 'next/head';
  9. import { useCurrentGrowiLayoutFluidClassName } from '~/client/services/layout';
  10. import { MainPane } from '~/components/Layout/MainPane';
  11. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  12. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  13. import { Page } from '~/components/Page';
  14. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  15. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  16. import { CrowiRequest } from '~/interfaces/crowi-request';
  17. import { RendererConfig } from '~/interfaces/services/renderer';
  18. import { IShareLinkHasId } from '~/interfaces/share-link';
  19. import {
  20. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  21. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri, useIsContainerFluid,
  22. } from '~/stores/context';
  23. import loggerFactory from '~/utils/logger';
  24. import { NextPageWithLayout } from '../_app.page';
  25. import {
  26. CommonProps, getServerSideCommonProps, generateCustomTitle, getNextI18NextConfig,
  27. } from '../utils/commons';
  28. const logger = loggerFactory('growi:next-page:share');
  29. const PageSideContents = dynamic(() => import('~/components/PageSideContents').then(mod => mod.PageSideContents), { ssr: false });
  30. // const Comments = dynamic(() => import('~/components/Comments').then(mod => mod.Comments), { ssr: false });
  31. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  32. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  33. type Props = CommonProps & {
  34. shareLink?: IShareLinkHasId,
  35. isExpired: boolean,
  36. disableLinkSharing: boolean,
  37. isSearchServiceConfigured: boolean,
  38. isSearchServiceReachable: boolean,
  39. isSearchScopeChildrenAsDefault: boolean,
  40. drawioUri: string | null,
  41. rendererConfig: RendererConfig,
  42. };
  43. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  44. useIsSearchPage(false);
  45. useShareLinkId(props.shareLink?._id);
  46. useCurrentPageId(props.shareLink?.relatedPage._id);
  47. useCurrentUser(props.currentUser);
  48. useCurrentPathname(props.currentPathname);
  49. useRendererConfig(props.rendererConfig);
  50. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  51. useIsSearchServiceReachable(props.isSearchServiceReachable);
  52. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  53. useDrawioUri(props.drawioUri);
  54. useIsContainerFluid(props.isContainerFluid);
  55. const growiLayoutFluidClass = useCurrentGrowiLayoutFluidClassName();
  56. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  57. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  58. const shareLink = props.shareLink;
  59. const title = generateCustomTitle(props, 'GROWI');
  60. const sideContents = shareLink != null
  61. ? <PageSideContents page={shareLink.relatedPage} />
  62. : <></>;
  63. // const footerContents = shareLink != null && isPopulated(shareLink.relatedPage.revision)
  64. // ? (
  65. // <>
  66. // <Comments pageId={shareLink._id} pagePath={shareLink.relatedPage.path} revision={shareLink.relatedPage.revision} />
  67. // </>
  68. // )
  69. // : <></>;
  70. return (
  71. <>
  72. <Head>
  73. <title>{title}</title>
  74. </Head>
  75. <div className={`dynamic-layout-root ${growiLayoutFluidClass} h-100 d-flex flex-column justify-content-between`}>
  76. <header className="py-0 position-relative">
  77. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  78. </header>
  79. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  80. <MainPane
  81. sideContents={sideContents}
  82. // footerContents={footerContents}
  83. >
  84. { props.disableLinkSharing && (
  85. <div className="mt-4">
  86. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  87. </div>
  88. )}
  89. { (isNotFound && !props.disableLinkSharing) && (
  90. <div className="container-lg">
  91. <h2 className="text-muted mt-4">
  92. <i className="icon-ban" aria-hidden="true" />
  93. <span> Page is not found</span>
  94. </h2>
  95. </div>
  96. )}
  97. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  98. <div className="container-lg">
  99. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  100. <h2 className="text-muted mt-4">
  101. <i className="icon-ban" aria-hidden="true" />
  102. <span> Page is expired</span>
  103. </h2>
  104. </div>
  105. )}
  106. {(isShowSharedPage && shareLink != null) && (
  107. <>
  108. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  109. <Page />
  110. </>
  111. )}
  112. </MainPane>
  113. </div>
  114. </>
  115. );
  116. };
  117. SharedPage.getLayout = function getLayout(page) {
  118. return (
  119. <>
  120. <DrawioViewerScript />
  121. <ShareLinkLayout>{page}</ShareLinkLayout>
  122. </>
  123. );
  124. };
  125. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  126. const req: CrowiRequest = context.req as CrowiRequest;
  127. const { crowi } = req;
  128. const { configManager, searchService, xssService } = crowi;
  129. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  130. props.isSearchServiceConfigured = searchService.isConfigured;
  131. props.isSearchServiceReachable = searchService.isReachable;
  132. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  133. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  134. props.rendererConfig = {
  135. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  136. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  137. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  138. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  139. plantumlUri: process.env.PLANTUML_URI ?? null,
  140. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  141. // XSS Options
  142. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  143. attrWhiteList: xssService.getAttrWhiteList(),
  144. tagWhiteList: xssService.getTagWhiteList(),
  145. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  146. };
  147. }
  148. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  149. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  150. props._nextI18Next = nextI18NextConfig._nextI18Next;
  151. }
  152. function getAction(props: Props): SupportedActionType {
  153. let action: SupportedActionType;
  154. if (props.isExpired) {
  155. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  156. }
  157. else if (props.shareLink == null) {
  158. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  159. }
  160. else {
  161. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  162. }
  163. return action;
  164. }
  165. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  166. const req: CrowiRequest = context.req as CrowiRequest;
  167. const parameters = {
  168. ip: req.ip,
  169. endpoint: req.originalUrl,
  170. action,
  171. user: req.user?._id,
  172. snapshot: {
  173. username: req.user?.username,
  174. },
  175. };
  176. await req.crowi.activityService.createActivity(parameters);
  177. }
  178. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  179. const req = context.req as CrowiRequest<IUserHasId & any>;
  180. const { crowi, params } = req;
  181. const result = await getServerSideCommonProps(context);
  182. if (!('props' in result)) {
  183. throw new Error('invalid getSSP result');
  184. }
  185. const props: Props = result.props as Props;
  186. try {
  187. const ShareLinkModel = crowi.model('ShareLink');
  188. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  189. if (shareLink != null) {
  190. await shareLink.relatedPage.populateDataToShowRevision();
  191. props.isExpired = shareLink.isExpired();
  192. props.shareLink = shareLink.toObject();
  193. }
  194. }
  195. catch (err) {
  196. logger.error(err);
  197. }
  198. injectServerConfigurations(context, props);
  199. await injectNextI18NextConfigurations(context, props);
  200. await addActivity(context, getAction(props));
  201. return {
  202. props,
  203. };
  204. };
  205. export default SharedPage;